package com.vision.sales.customer.cl.ui;

import static com.vision.sales.cl.SalesServices.customerSvc;

import com.allen_sauer.gwt.log.client.Log;
import com.google.gwt.core.client.GWT;
import com.google.gwt.editor.client.Editor;
import com.google.gwt.editor.client.SimpleBeanEditorDriver;
import com.google.gwt.i18n.client.NumberFormat;
import com.google.gwt.uibinder.client.UiBinder;
import com.google.gwt.uibinder.client.UiFactory;
import com.google.gwt.uibinder.client.UiField;
import com.google.gwt.uibinder.client.UiHandler;
import com.google.gwt.user.client.ui.IsWidget;
import com.google.gwt.user.client.ui.Widget;
import com.sencha.gxt.widget.core.client.Window;
import com.sencha.gxt.widget.core.client.button.TextButton;
import com.sencha.gxt.widget.core.client.container.VerticalLayoutContainer;
import com.sencha.gxt.widget.core.client.event.SelectEvent;
import com.sencha.gxt.widget.core.client.form.CheckBox;
import com.sencha.gxt.widget.core.client.form.NumberField;
import com.sencha.gxt.widget.core.client.form.NumberPropertyEditor;
import com.sencha.gxt.widget.core.client.form.NumberPropertyEditor.DoublePropertyEditor;
import com.sencha.gxt.widget.core.client.form.SimpleComboBox;
import com.sencha.gxt.widget.core.client.form.TextArea;
import com.sencha.gxt.widget.core.client.form.TextField;
import com.sencha.gxt.widget.core.client.form.validator.MaxLengthValidator;
import com.vision.core.cl.BaseModule;
import com.vision.core.cl.Callback;
import com.vision.core.cl.ui.UI;
import com.vision.core.cl.ui.component.EnumComboBox;
import com.vision.sales.cl.event.CustomerCreatedEvent;
import com.vision.sales.cl.resource.SalesResources;
import com.vision.sales.cl.ui.component.SalesmanComboBox;
import com.vision.sales.cm.data.PaymentTerm;
import com.vision.sales.cm.db.data.Customer;
import com.vision.sales.cm.db.data.Customer.FieldSpecs;

/**
 * 
 * @author Mark
 *
 */
public class CreateCustomerWindow implements IsWidget, Editor<Customer> {
	
	interface Binder extends UiBinder<Widget, CreateCustomerWindow> { }
	
	private static final Binder binder = GWT.create(Binder.class);
	
	@UiField Window window;
	@UiField VerticalLayoutContainer mainPanel;
	
	// Form fields that are bound by the form driver to the fields in the Customer class.
	// The name of these form fields must match the name of the field in the Customer class. 
	@UiField TextField code;
	@UiField TextField name;
	@UiField CheckBox withholdingTax;
	@UiField TextField tin;
	@UiField NumberField<Double> taxRate;
	@UiField CheckBox taxRateDirect;
	@UiField SimpleComboBox<PaymentTerm> paymentTerm;
	@UiField SalesmanComboBox salesman;
	
	@UiField TextArea address;
	@UiField TextArea deliverySite;
	@UiField TextField email;
	@UiField TextField phone;
	@UiField TextField contactName;
	@UiField TextField contactPhone;
	
	@UiField CheckBox allowOverruns;
	@UiField NumberField<Double> overrunsRate;
	
	@UiField(provided = true)
	NumberPropertyEditor<Double> doublePropertyEditor = new DoublePropertyEditor();
	
	@UiField(provided = true)
	NumberFormat numberFormat = NumberFormat.getFormat("0.00");
	
	@Ignore @UiField TextButton saveBtn;
	@Ignore @UiField TextButton cancelBtn;
	
	interface CustomerDriver extends SimpleBeanEditorDriver<Customer, CreateCustomerWindow> { }
	
	private CustomerDriver driver = GWT.create(CustomerDriver.class); // Used to bind the grid to the form editor
	
	// %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
	@Override
	public Widget asWidget() {
		window = (Window) binder.createAndBindUi(this);
		window.getHeader().setIcon(SalesResources.INSTANCE.customers());
		
		driver.initialize(this);
		driver.edit(new Customer());
		
		code.addValidator(new MaxLengthValidator(FieldSpecs.CODE_MAX));
		name.addValidator(new MaxLengthValidator(FieldSpecs.NAME_MAX));
		address.addValidator(new MaxLengthValidator(FieldSpecs.ADDRESS_MAX));
		deliverySite.addValidator(new MaxLengthValidator(FieldSpecs.DELIVERY_SITE_MAX));
		email.addValidator(new MaxLengthValidator(FieldSpecs.EMAIL_MAX));
		phone.addValidator(new MaxLengthValidator(FieldSpecs.PHONE_MAX));
//		fax.addValidator(new MaxLengthValidator(FieldSpecs.FAX_MAX));
		contactName.addValidator(new MaxLengthValidator(FieldSpecs.CONTACT_MAX));
		contactPhone.addValidator(new MaxLengthValidator(FieldSpecs.CONTACT_PHONE_MAX));
		tin.addValidator(new MaxLengthValidator(FieldSpecs.TIN_MAX));
		
		loadData();
		
		return window;
	}
	
	// UiFactory Methods %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%
	
	@Ignore
	@UiFactory
	SimpleComboBox<PaymentTerm> initializePaymentTermCombo() {
		return new EnumComboBox<PaymentTerm>(PaymentTerm.values());
	}
	
	// Logic %%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%%

	public void loadData() {
		Log.debug("Loading salesmen for dropdown...");
		salesman.load();
	}
	
	@UiHandler("saveBtn")
	void saveClicked(SelectEvent e) {
		Customer customer = driver.flush();
		if (driver.hasErrors())
			return;
		
		UI.mask(window, "Creating customer...");
		customerSvc().create(customer, new Callback<Customer>(window) {
			@Override
			protected void handleSuccess(Customer result) {
				window.hide(saveBtn);
				BaseModule.fireEvent(new CustomerCreatedEvent(result));
			}
		});
	}
	
	@UiHandler("cancelBtn")
	void cancelClicked(SelectEvent e) {
		window.hide(cancelBtn);
	}

}
